package com.bskj.kuaifu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bskj.common.bean.model.WxTemplate;
import com.bskj.common.dao.WxTemplateDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.service.impl.BaseServiceImpl;
import com.bskj.common.utils.StringUtils;
import com.bskj.kuaifu.service.IWxTemplateService;

/**
 * Created by mocaijin on 21/12/2017.
 */
@Service
public class WxTemplateServiceImpl extends BaseServiceImpl<WxTemplate> implements IWxTemplateService {

    @Autowired
    private WxTemplateDao wxTemplateDao;

    @Override
    public WxTemplate getByKey(String key) {
        return wxTemplateDao.getByKey(key);
    }

    @Override
    public WxTemplate get(WxTemplate wxTemplate) throws Exception {
        
        FindRequestEntity findReq = new FindRequestEntity();
        if(!StringUtils.isBank(wxTemplate.getAppId())){
            findReq.addWhere("app_id", WhereEnum.EQ, wxTemplate.getAppId());
        }

        if(!StringUtils.isBank(wxTemplate.getTemplateKey())){
            findReq.addWhere("template_key", WhereEnum.EQ, wxTemplate.getTemplateKey());
        }
        
        List<WxTemplate> wxTemplateList = find(findReq);
        if (wxTemplateList != null && wxTemplateList.size() > 0) {
            return wxTemplateList.get(0);
        }
        return null;
    }

    @Override
    public BaseDao<WxTemplate> getBaseDao() {
        return wxTemplateDao;
    }
}
